/**
 * 按照年份统计问题设备数量
 */
var quesCountByYearChart = echarts.init(document.getElementById("allQuesCount"));
/**
 * 按照分子公司（acceptScope）统计某一年的问题设备数量
 */
var quesCountByAcceptScopeChart = echarts.init(document.getElementById("acceptScopeQuesCount"));
/**
 * 按照供电局统计某一年的某个分子公司的问题设备数量
 */
var quesCountByAcceptUnitChart = echarts.init(document.getElementById("acceptUnitQuesCount"));
/**
 * 查看的年份
 */
var clickYear;
/**
 * 查看的分子公司
 */
var clickAcceptScope;

$(function() {
	initQueryYear();
	initChartData();
});

function initQueryYear() {
	var d = new Date();
	var currentYear=d.getFullYear();
	$('#firstYear').val(currentYear-1);
	$('#firstYear').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
	$('#lastYear').val(currentYear);
	$('#lastYear').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
}

function initChartData() {
	var startYear = $("#firstYear").val();
	var endYear = $("#lastYear").val();
	$.ajaxSettings.async = false;
	getQuesEquipmentCountDataByYear(startYear,endYear);
	getQuesEquipmentCountDataByScope(startYear);
	clickYear = startYear;
	initQuesEquipmentCountByUnit(clickYear,clickAcceptScope);
	$.ajaxSettings.async = true;
}

/**
 * 按年份统计不同等级的问题设备数量
 * @returns
 */
function getQuesEquipmentCountDataByYear(startYear,endYear) {
	if ($("#yearTitle").empty()) {
		$("#yearTitle").append(startYear + "-" + endYear + "年全网问题设备类型统计");
	} 
	$.getJSON('/rest/quesequipment/getQuesEquipmentByYear/' + startYear + '/' + endYear, function(result) {
		processYearCountData(result);
	});
}

/**
 * 将后台传递的数据转化为前台可直接展示的数据格式
 * @param dates
 * @returns
 */
function processYearCountData(dates) {
	if(dates.length==0) {
		$("#allQuesCount").html("该时间暂无数据信息");
	} else {
		var riskLevels = [];
		var counts = new Array()
		var years = [];
		// count:6 year:2020 riskLevel:一级
		for(var i in dates) {
			var year = dates[i].groupType;
			var riskLevel = dates[i].riskLevel;
			var count = dates[i].count;
			if(years.indexOf(year) == -1){
				years.push(year);
			}
			if(riskLevels.indexOf(riskLevel) == -1){
				riskLevels.push(riskLevel);
				var value = [];
				value.push(count);
				counts[i] = value;
			} else {
				var value = [];
				value = counts[i-riskLevels.length*(years.length-1)];
				value.push(count);
				counts[i-riskLevels.length*(years.length-1)] = value;
			}
		}
		var seriesData = [];
		for(var i in riskLevels) {
			var data = {name: riskLevels[i], type: 'bar', stack: '总量', label: {show: true,position: 'insideRight'},data: counts[i]};
			seriesData.push(data);
		}
		genQuesCountStackedBar(quesCountByYearChart,years,seriesData,riskLevels,'年份');
	}
}
/**
 * 点击年统计数据时，展示该年份下各个分子公司的问题设备情况
 * @param params
 * @returns
 */
quesCountByYearChart.on("click",function(params) {
	clickYear = params.name;
	$.ajaxSettings.async = false;
	getQuesEquipmentCountDataByScope(clickYear);
	initQuesEquipmentCountByUnit(clickYear,clickAcceptScope);
	$.ajaxSettings.async = true;
});

function getQuesEquipmentCountDataByScope(year) {
	if ($("#acceptScopeTitle").empty()) {
		$("#acceptScopeTitle").append(year + "年各分子公司问题设备统计");
	} 
	$.getJSON('/rest/quesequipment/getQuesEquipmentGroupByComp/' + year, function(result) {
		processAcceptScopeCountData(result);
	});
}

function processAcceptScopeCountData(dates) {
//	if(dates.length==0) {
//		$("#acceptScopeQuesCount").html("该时间暂无数据信息");
//	} else {
		var riskLevels = [];
		var counts = new Array()
		var acceptScopes = [];
		for(var i in dates) {
			var acceptScope = dates[i].groupType;
			var riskLevel = dates[i].riskLevel;
			var count = dates[i].count;
			if(acceptScopes.indexOf(acceptScope) == -1){
				acceptScopes.push(acceptScope);
			}
			if(riskLevels.indexOf(riskLevel) == -1){
				riskLevels.push(riskLevel);
				var value = [];
				value.push(count);
				counts[i] = value;
			} else {
				var value = [];
				value = counts[i-riskLevels.length*(acceptScopes.length-1)];
				value.push(count);
				counts[i-riskLevels.length*(acceptScopes.length-1)] = value;
			}
		}
		var seriesData = [];
		for(var i in riskLevels) {
			var data = {name: riskLevels[i], type: 'bar', stack: '总量', label: {show: true,position: 'insideRight'},data: counts[i]};
			seriesData.push(data);
		}
		genQuesCountStackedBar(quesCountByAcceptScopeChart,acceptScopes,seriesData,riskLevels,'分子公司');
		clickAcceptScope = acceptScopes[0];
//	}
}

/**
 * 点击分子公司的统计数据时，展示该分子公司下所有供电局的问题设备统计情况
 * @param params
 * @returns
 */
quesCountByAcceptScopeChart.on("click",function(params) {
	clickAcceptScope = params.name;
	initQuesEquipmentCountByUnit(clickYear,clickAcceptScope);
});

function initQuesEquipmentCountByUnit(year,acceptScope) {
	if ($("#acceptUnitTitle").empty()) {
		if(typeof(acceptScope) == "undefined") {
			$("#acceptUnitTitle").append(year + "年各供电局问题设备统计");
		} else {
			$("#acceptUnitTitle").append(year + "年"+acceptScope+"各供电局问题设备统计");
		}
	} 
	$.getJSON('/rest/quesequipment/getQuesEquipmentCountByUnit/' + year + '/' + acceptScope, function(result) {
		processAcceptUnitCountData(result);
	});
}
/**
 * 处理数据
 * @param dates
 * @returns
 */
function processAcceptUnitCountData(dates) {
//	if(dates.length==0) {
//		$("#acceptUnitQuesCount").html("该时间暂无数据信息");
//	} else {
		var riskLevels = [];
		var counts = new Array()
		var xDatas = [];
		for(var i in dates) {
			var acceptUnit = dates[i].groupType;
			var riskLevel = dates[i].riskLevel;
			var count = dates[i].count;
			if(xDatas.indexOf(acceptUnit) == -1){
				xDatas.push(acceptUnit);
			}
			if(riskLevels.indexOf(riskLevel) == -1){
				riskLevels.push(riskLevel);
				var value = [];
				value.push(count);
				counts[i] = value;
			} else {
				var value = [];
				value = counts[i-riskLevels.length*(xDatas.length-1)];
				value.push(count);
				counts[i-riskLevels.length*(xDatas.length-1)] = value;
			}
		}
		var seriesData = [];
		for(var i in riskLevels) {
			var data = {name: riskLevels[i], type: 'bar', stack: '总量', label: {show: true,position: 'insideRight'},data: counts[i]};
			seriesData.push(data);
		}
		genQuesCountStackedBar(quesCountByAcceptUnitChart,xDatas,seriesData,riskLevels,'供电局');
//	}
}
/**
 * 渲染堆叠柱状图
 * @param qechart		对应的统计图
 * @param xData			横轴数据
 * @param seriesData
 * @param legendData
 * @param xAxisName		横轴名称
 * @returns
 */
function genQuesCountStackedBar(qechart,xData,seriesData,legendData,xAxisName) {
	var option = {
	    tooltip: {
	        trigger: 'axis',
	        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
	            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
	        }
	    },
	    toolbox: {
	        show: true,
	        feature: {
	            dataView: {show: true, readOnly: false},
	            magicType: {show: true, type: ['line', 'bar']},
	            restore: {show: true},
	            saveAsImage: {show: true}
	        }
	    },
	    legend: {
	        data: legendData
	    },
	    grid: {
	        left: '3%',
	        right: '4%',
	        bottom: '3%',
	        containLabel: true
	    },
	    yAxis: {
	    	name: '问题设备数量',
	        type: 'value'
	    },
	    xAxis: {
	    	name: xAxisName,
	        type: 'category',
	        data: xData
	    },
	    series: seriesData
	};
	qechart.setOption(option);
}
